//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.tool.utils;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.MemoryInfo;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.app.AlertDialog.Builder;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Bitmap.Config;
import android.graphics.drawable.Drawable;
import android.media.ThumbnailUtils;
import android.net.ConnectivityManager;
import android.net.Uri;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Environment;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.m7788.util.AppUtil;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.List;
import org.json.JSONException;
import org.json.JSONObject;

public class DataUtil {
    private static boolean flag = false;
    public static boolean showFlag = false;

    public DataUtil() {
    }

    public static void dex() {
        switch(0) {
            case 1001:
                String var1 = null;

                try {
                    JSONObject jb = new JSONObject("");
                    var1 = jb.getString("aa");
                } catch (JSONException var3) {
                    var3.printStackTrace();
                }
            default:
        }
    }

    public static void start() {
        flag = true;
    }

    public static void saveRecord(Context context, String key, String value) {
        AppUtil.saveRecord(context,key, value);
    }

    public static String getRecord(Context context, String key) {
        String result = AppUtil.getRecord(context,key);
        return result;
    }

    public static void showShortToast(Context context, String message) {
        Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
    }

    public static void showLongToast(Context context, String message) {
        Toast.makeText(context, message, Toast.LENGTH_LONG).show();
    }

    public static void Alert(Context context, String message) {
        Builder builder = new Builder(context);
        builder.setMessage(message).setCancelable(false).setPositiveButton("确定", new OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
            }
        });
        builder.show();
    }

    public static void AlertAndFinish(Context context, String message, final Activity activity) {
        Builder builder = new Builder(context);
        builder.setMessage(message).setCancelable(false).setPositiveButton("确定", new OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
                activity.finish();
            }
        });
        builder.show();
    }

    public static String getMobileImsi(Context context) {
        TelephonyManager tm = (TelephonyManager)context.getSystemService("phone");
        String imsi = tm.getSubscriberId();
        return imsi;
    }

    public static String getDeviceId(Context context) {
        TelephonyManager tm = (TelephonyManager)context.getSystemService("phone");
        String tmDevice = tm.getDeviceId();
        if(tmDevice.length() == 0) {
            tmDevice = getSerial();
        }

        return tmDevice;
    }

    public static String getSerial() {
        String serial = null;

        try {
            Class<?> c = Class.forName("android.os.SystemProperties");
            Method get = c.getMethod("get", new Class[]{String.class});
            serial = (String)get.invoke(c, new Object[]{"ro.serialno"});
        } catch (Exception var3) {
            var3.printStackTrace();
        }

        return serial;
    }

    public static String getAppVersionName(Context context) {
        String versionName = "";

        try {
            PackageManager pm = context.getPackageManager();
            PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
            versionName = pi.versionName;
        } catch (Exception var4) {
            ;
        }

        return versionName;
    }

    public static String getAppVersionCode(Context context) {
        String versionCode = "";

        try {
            PackageManager pm = context.getPackageManager();
            PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
            versionCode = String.valueOf(pi.versionCode);
        } catch (Exception var4) {
            ;
        }

        return versionCode;
    }

    public static boolean getOpen() {
        return flag;
    }

    public static void setShowLog(boolean bool) {
        showFlag = bool;
    }

    public static boolean isConnectNet(Context context) {
        boolean netSataus = false;
        ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService("connectivity");
        connectivityManager.getActiveNetworkInfo();
        if(connectivityManager.getActiveNetworkInfo() != null) {
            netSataus = connectivityManager.getActiveNetworkInfo().isAvailable();
        }

        return netSataus;
    }

    public static boolean isWiFiActive(Context context) {
        WifiManager mWifiManager = (WifiManager)context.getSystemService("wifi");
        WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
        int ipAddress = wifiInfo == null?0:wifiInfo.getIpAddress();
        return mWifiManager.isWifiEnabled() && ipAddress != 0;
    }

    public static String getScreenWidth(Activity activity) {
        DisplayMetrics dm = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
        return String.valueOf(dm.widthPixels);
    }

    public static String getScreenHeight(Activity activity) {
        DisplayMetrics dm = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
        return String.valueOf(dm.heightPixels);
    }

    public static long getAvailMemory(Context context) {
        ActivityManager am = (ActivityManager)context.getSystemService("activity");
        MemoryInfo mi = new MemoryInfo();
        am.getMemoryInfo(mi);
        return mi.availMem / 1048576L;
    }

    public static long getTotalMemory(Context context) {
        String str1 = "/proc/meminfo";
        long initial_memory = 0L;

        try {
            FileReader localFileReader = new FileReader(str1);
            BufferedReader localBufferedReader = new BufferedReader(localFileReader, 8192);
            String str2 = localBufferedReader.readLine();
            String[] arrayOfString = str2.split("\\s+");
            String[] var11 = arrayOfString;
            int var10 = arrayOfString.length;

            for(int var9 = 0; var9 < var10; ++var9) {
                String num = var11[var9];
                Log.i(str2, num + "\t");
            }

            initial_memory = (long)(Integer.valueOf(arrayOfString[1]).intValue() * 1024);
            localBufferedReader.close();
        } catch (IOException var12) {
            ;
        }

        return initial_memory / 1048576L;
    }

    public static File createSDFolder(String folderName) {
        File file = null;
        boolean sdCardExist = Environment.getExternalStorageState().equals("mounted");
        if(sdCardExist) {
            file = new File(Environment.getExternalStorageDirectory(), folderName);
            if(!file.exists()) {
                file.mkdirs();
            }
        }

        return file;
    }

    public static void deleSDFolder(String folderName) {
        boolean sdCardExist = Environment.getExternalStorageState().equals("mounted");
        if(sdCardExist) {
            File file = new File(Environment.getExternalStorageDirectory(), folderName);
            if(file.exists()) {
                if(file.listFiles()!=null&&file.listFiles().length > 0) {
                    File[] var6;
                    int var5 = (var6 = file.listFiles()).length;

                    for(int var4 = 0; var4 < var5; ++var4) {
                        File temp = var6[var4];
                        if(temp.isDirectory()) {
                            deleSDFolder(temp);
                        } else {
                            temp.delete();
                        }
                    }
                }

                file.delete();
            }
        }

    }

    public static void deleSDFolder(File file) {
        if(file.exists()) {
            if(file.listFiles()!=null&&file.listFiles().length > 0) {
                File[] var4;
                int var3 = (var4 = file.listFiles()).length;

                for(int var2 = 0; var2 < var3; ++var2) {
                    File temp = var4[var2];
                    if(temp.isDirectory()) {
                        deleSDFolder(temp);
                    } else {
                        temp.delete();
                    }
                }
            }

            file.delete();
        }

    }

    public static void deleFileInFolder(File file) {
        if(file.exists() && file.listFiles()!=null && file.listFiles().length > 0) {
            File[] var4;
            int var3 = (var4 = file.listFiles()).length;

            for(int var2 = 0; var2 < var3; ++var2) {
                File temp = var4[var2];
                if(temp.isDirectory()) {
                    deleSDFolder(temp);
                } else {
                    temp.delete();
                }
            }
        }

    }

    public static void openApp(String packageName, Context context) {
        if(!StrUtils.isEmpty(packageName)) {
            PackageManager packageManager = context.getPackageManager();
            Intent resolveIntent = new Intent("android.intent.action.MAIN", (Uri)null);
            resolveIntent.addCategory("android.intent.category.LAUNCHER");
            resolveIntent.setPackage(packageName);
            List<ResolveInfo> apps = packageManager.queryIntentActivities(resolveIntent, 0);
            if(apps != null) {
                ResolveInfo ri = (ResolveInfo)apps.iterator().next();
                if(ri != null) {
                    String className = ri.activityInfo.name;
                    Intent intent = new Intent("android.intent.action.MAIN");
                    intent.addCategory("android.intent.category.LAUNCHER");
                    intent.addFlags(268435456);
                    ComponentName cn = new ComponentName(packageName, className);
                    intent.setComponent(cn);
                    context.startActivity(intent);
                }
            }

        }
    }

    public static boolean isHaveApp(String packageName, Context context) {
        if(packageName != null && !"".equals(packageName)) {
            try {
                ApplicationInfo info = context.getPackageManager().getApplicationInfo(packageName, 8192);
                return true;
            } catch (NameNotFoundException var3) {
                return false;
            }
        } else {
            return false;
        }
    }

    public static void deleteApp(String packageName, Context context) {
        Uri packageURI = Uri.parse("package:" + packageName);
        Intent uninstallIntent = new Intent("android.intent.action.DELETE", packageURI);
        uninstallIntent.addFlags(268435456);
        context.startActivity(uninstallIntent);
    }

    public static String htmlDecode(String source) {
        String html = "";
        if(source != null && source.length() > 0) {
            html = source.replaceAll("&amp;", "&").replaceAll("&lt;", "<").replaceAll("&gt;", ">").replaceAll("&quot;", "\"").replaceAll("&nbsp;", " ").replaceAll("<br/>", "\n").replaceAll("<br/>", "\r");
        }

        return html;
    }

    public static String clearHtml(String html) {
        html = html.replaceAll("<[^>]*>", "");
        html = html.replaceAll("<[^>]*/>", "");
        html = html.replaceAll("&nbsp;", "");
        html = html.trim();
        return html;
    }

    public static void clearMemory(Context context) {
        ActivityManager activityManger = (ActivityManager)context.getSystemService("activity");
        List<RunningAppProcessInfo> list = activityManger.getRunningAppProcesses();
        if(list != null) {
            for(int i = 0; i < list.size(); ++i) {
                RunningAppProcessInfo apinfo = (RunningAppProcessInfo)list.get(i);
                String[] pkgList = apinfo.pkgList;
                if(apinfo.importance > 300) {
                    for(int j = 0; j < pkgList.length; ++j) {
                        activityManger.killBackgroundProcesses(pkgList[j]);
                    }
                }
            }
        }

    }

    public static Bitmap getBitmapThumbnail(Bitmap source, int width, int height) {
        return ThumbnailUtils.extractThumbnail(source, width, height);
    }

    public static Bitmap drawableToBitmap(Drawable drawable) {
        Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), drawable.getOpacity() != -1?Config.ARGB_8888:Config.RGB_565);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        drawable.draw(canvas);
        return bitmap;
    }

    public static void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if(listAdapter != null) {
            int totalHeight = 0;
            int i = 0;

            for(int len = listAdapter.getCount(); i < len; ++i) {
                View listItem = listAdapter.getView(i, (View)null, listView);
                listItem.measure(0, 0);
                totalHeight += listItem.getMeasuredHeight();
            }

            LayoutParams params = listView.getLayoutParams();
            params.height = totalHeight + listView.getDividerHeight() * (listAdapter.getCount() - 1);
            listView.setLayoutParams(params);
        }
    }

    public static int dip2px(Context context, float dpValue) {
        float scale = context.getResources().getDisplayMetrics().density;
        return (int)(dpValue * scale + 0.5F);
    }

    public static int px2dip(Context context, float pxValue) {
        float scale = context.getResources().getDisplayMetrics().density;
        return (int)(pxValue / scale + 0.5F);
    }
}
